<?php

namespace app\admin\controller;

use app\admin\model\AuthGroup;
use app\common\controller\Backend;
use think\Config;
use think\Session;
use app\admin\model\AuthGroupAccess;
use app\admin\model\Article;
use app\admin\model\Plan;
use app\admin\model\Record;
use app\admin\model\Project;
use app\admin\model\Task;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    protected $model = null;
    protected $noNeedRight = ['detail'];
    protected $admin_id;
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\Dashboard();
        $this->admin_id = Session::get("admin")['id'];
    }

    /**
     * 查看
     */
    public function index()
    {
        //待办任务
        $task = Task::where(["assign_id"=>$this->admin_id,"state"=>0])->count();
        //待做项目
        $project = Project::where(["user_id"=>$this->admin_id,"status"=>0])->count();
        //未完日志
        $record = Record::where(["admin_id"=>$this->admin_id,"status"=>0])->count();

        //项目完成比
        //已完成项目
        $projectPercentageA = Project::where(["status"=>1])->count();
        $projectPercentageB = Project::count();
        if($projectPercentageB){
            $projectPercentage = round($projectPercentageA/$projectPercentageB,2)*100;
        }else{
            $projectPercentage = 0;
        }

        //日志提交比
        $whereRecordPercentageA['createtime'] = ['between time',[date("Y-m-d",time()),date("Y-m-d",time()).' 23:23:59']];
        $recordPercentageA = Record::where($whereRecordPercentageA)->count();
        $recordPercentage = round($recordPercentageA/7,2)*100;

        //任务完成率
        $taskFinishPercentageA = Task::where(["assign_id"=>$this->admin_id,"state"=>1])->count();
        $taskFinishPercentageB = Task::where(["assign_id"=>$this->admin_id])->count();
        if($taskFinishPercentageB){
            $taskFinishPercentage = round($taskFinishPercentageA/$taskFinishPercentageB,2)*100;
        }else{
            $taskFinishPercentage = 0;
        }

        //日志完成率
        $recordFinishPercentageA = Record::where(["admin_id"=>$this->admin_id,"status"=>1])->count();
        $recordFinishPercentageB = Record::where(["admin_id"=>$this->admin_id])->count();
        if($recordFinishPercentageB){
            $recordFinishPercentage = round($recordFinishPercentageA/$recordFinishPercentageB,2)*100;
        }else{
            $recordFinishPercentage = 0;
        }

        $this->view->assign([
            'task'=>$task,
            'project'=>$project,
            'record'=>$record,
            'projectPercentageA'=>$projectPercentageA,
            'projectPercentageB'=>$projectPercentageB,
            'projectPercentage'=>$projectPercentage,
            'recordPercentageA'=>$recordPercentageA,
            'recordPercentageB'=>7,
            'recordPercentage'=>$recordPercentage,
            'taskFinishPercentage'=>$taskFinishPercentage,
            'recordFinishPercentage'=>$recordFinishPercentage,
        ]);
        //$this->view->engine->layout(false);
        return $this->view->fetch();
    }

    public function detail(){
        $id=input("get.ids");
        $xjlist =  db('article')->where("id",$id)->find();
        $this->assign('xjlist',$xjlist);
        return $this->fetch();
    }


}
